#include "dialog_login.h"
#include "ui_dialog_login.h"
#include <QSqlDatabase>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QTime>
#include <QTimer>
#include <QDebug>

#include "passwd.h"
#include "loginManage.h"


Dialog_Login::Dialog_Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_Login)
{
    ui->setupUi(this);
    this->setWindowModality(Qt::WindowModal);
    setWindowFlags(Qt::FramelessWindowHint);
    setWindowTitle(tr("权限认证"));//窗口标题
    box = nullptr;

    ui->lineEdit_passwd->setEchoMode(QLineEdit::Password);
    QDialog::setModal(true);//设置模态，让该窗口一直在最前端

    loginUser = new LoginUser();


    ui->comboBox_user_name->setEditText("jyuser1");
    createUserDB();
}

Dialog_Login::~Dialog_Login()
{
    if(nullptr != box){
        delete box;
    }
    delete ui;
}

bool Dialog_Login::createUserDB()
{
    loginDB = new LoginDB();
    loginDB->createLoginDB();
    ui->comboBox_user_name->addItems(loginDB->getUsersList());
    return true;
}

void Dialog_Login::userShow()
{
    QTimer::singleShot(200,this,SLOT(updateTopImage()));
    this->show();
}

void Dialog_Login::setTopImage(bool isSandom, int index_img)
{
    int index = getImageIndex(isSandom,index_img);
    QImage top_img;
    QString top_img_path = QString(":/images/image/top_img%1.png").arg(index);

    top_img.load(top_img_path);
    int width = ui->label_top_image->width();
    int height = ui->label_top_image->height();
    QPixmap top_pic = QPixmap::fromImage(top_img.scaled(width,height));
    ui->label_top_image->setPixmap(top_pic);
}

void Dialog_Login::setUserImage(bool isSandom, int index_img)
{
    int index = getImageIndex(isSandom,index_img);
    QImage user_img;
    QString user_img_path = QString(":/images/image/nurse%1.png").arg(index);

    user_img.load(user_img_path);
    qint32 width = ui->label_user_image->width();
    qint32 height = ui->label_user_image->height();
    QPixmap pixmap = QPixmap::fromImage(user_img.scaled(width,height));
    ui->label_user_image->setPixmap(pixmap);
}

int Dialog_Login::getImageIndex(bool isSandom, int index)
{
    int set_index_img = 1;
    if(isSandom == true)//随机显示userimg
    {
        QTime time_sand;
        time_sand= QTime::currentTime();//获取当前时间
        qsrand(static_cast<uint>(time_sand.msec() + time_sand.second()*1000));
        set_index_img = qrand()%5;
    }
    else
    {
        set_index_img = index;
    }
    return set_index_img;
}

bool Dialog_Login::login()
{
    loginUser->setName(ui->comboBox_user_name->currentText());
    loginUser->setPasswd(ui->lineEdit_passwd->text());
    if(loginUser->checkEmpty())
    {
        box = new MessageBox(this,
                             MessageBox::Error,
                             "认证失败",
                             "请输入密码!",
                             QMessageBox::Ok, QMessageBox::Ok);
        box->exec();
        if(box){
            box->deleteLater();
        }
        return false;
    }

    QSqlTableModel model;
    model.setTable("user");
    model.select();
    for(int i=0;i<model.rowCount();++i)
    {
        QSqlRecord record = model.record(i);
        QString name = record.value("name").toString();
        QString passWord = record.value("passwd").toString();
        if(loginUser->getName() == name){
            if(ui->lineEdit_passwd->text() == passWord){
                qint32 level = record.value("level").toInt();
                qDebug() << "level=" << level;
                loginUser->setLevel(level);
                return true;
            }
        }
    }

    box = new MessageBox(this,
                         MessageBox::Error,
                         "认证失败",
                         "您输入的信息有误，请核对后输入!",
                         QMessageBox::Ok, QMessageBox::Ok);
    box->exec();
    if(box){
        box->deleteLater();
    }
    return false;
}

void Dialog_Login::on_pushButton_login_clicked()
{
    if(login()){
        emit signalLoginPass();
        this->close();
    }
}

void Dialog_Login::updateTopImage()
{
    setTopImage(true,1);
    setUserImage(true,1);
}


void Dialog_Login::on_comboBox_user_name_currentIndexChanged(int index)
{
    setTopImage(false,index);
    setUserImage(false,index);
}

LoginUser *Dialog_Login::getLoginUser() const
{
    return loginUser;
}

void Dialog_Login::on_pushButton_user_config_clicked()
{
    if(login()){
        if(0 != loginUser->getLevel()){
            box = new MessageBox(this,
                                 MessageBox::Error,
                                 "权限不足",
                                 "当前用户无管理权限!",
                                 QMessageBox::Ok, QMessageBox::Ok);
            box->exec();
            if(box){
                box->deleteLater();
            }
            return;
        }

        LoginManage *lm = new LoginManage();
        lm->setParent(this);
        lm->exec();
    }
}

void Dialog_Login::on_pushButton_passwd_change_clicked()
{
    static Passwd *passwd = nullptr;
    if(login()){
        if(!passwd){
            passwd = new Passwd();
            passwd->setDLogin(this);
            passwd->show();
            return;
        }
        passwd->show();
    }
}

LoginDB *Dialog_Login::getLoginDB() const
{
    return loginDB;
}
